package com.hyt.it.ogt.data.service.pj.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.data.mapper.pj.PjDataStudentMapper;
import com.hyt.it.ogt.data.mapper.pj.PjDataSubjectMapper;
import com.hyt.it.ogt.data.model.dto.NeedReduceDTO;
import com.hyt.it.ogt.data.model.dto.PjConfigCode;
import com.hyt.it.ogt.data.model.dto.StudentExtendDTO;
import com.hyt.it.ogt.data.model.entity.pj.Paper;
import com.hyt.it.ogt.data.model.entity.pj.Student;
import com.hyt.it.ogt.data.model.entity.pj.StudentConfig;
import com.hyt.it.ogt.data.service.pj.IPjDataPaperService;
import com.hyt.it.ogt.data.service.pj.IPjDataStudentConfigService;
import com.hyt.it.ogt.data.service.pj.IPjDataStudentService;
import com.hyt.it.ogt.pj.common.MarkException;
import com.hyt.it.ogt.pj.common.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考生表 服务实现类
 * </p>
 *
 * @author liuqi
 * @date 2022-06-07
 */
@Service
@Slf4j
public class PjDataStudentServiceImpl extends BaseServiceImpl<PjDataStudentMapper, Student> implements IPjDataStudentService {

	@Resource
    private IPjDataPaperService paperService;
	
	@Resource
    private IPjDataStudentConfigService studentConfigService;
	
	@Resource
    private PjDataStudentMapper pjDataStudentMapper;

	@Resource
	private PjDataSubjectMapper pjDataSubjectMapper;

	@Override
	public void saveStudent(String projectId, List<StudentExtendDTO> studentDTOs) {
		List<Student> insertStudents = new ArrayList<>();
		for (StudentExtendDTO studentDTO : studentDTOs) {
			if (null != studentDTO.getHasSubjective() && !studentDTO.getHasSubjective()) {
				continue;
			}
			// 处理考生个人信息数据
			saveStudentConfig(projectId, studentDTO);

			List<Paper> papers = paperService.selectParersBySubjectId(projectId, null);
			// 保存基础表
			Student student = new Student();
			BeanUtils.copyProperties(studentDTO, student);
			student.setId(null);
			for (Paper paper : papers) {
				String paperId = paper.getId();
				String sjpaperId = paper.getPaperId();
				Integer paperVersion = paper.getPaperVersion();
				if (paper.getPackageId() == null) {
					if (sjpaperId.equals(student.getPaperId()) && paperVersion == student.getPaperVersion()) {
						log.info("## 设置的试卷id为:{}", paper.getId());
						student.setPaperId(paperId);
						student.setPaperVersion(paperVersion);
						student.setPackageId(null);
						student.setPackageVersion(null);
						break;
					}
				} else {
					// 试卷包
					if (paper.getPackageId().equals(studentDTO.getPackageId()) && sjpaperId.equals(student.getPaperId()) && paperVersion.equals(student.getPaperVersion())) {
						log.info("## 设置的试卷包id为:{}", paper.getPackageId());
						student.setPaperId(paperId);
						student.setPaperVersion(paperVersion);
						student.setPackageId(paper.getPackageId());
						student.setPackageVersion(paper.getPackageVersion());
						break;
					}
				}
			}
			insertStudents.add(student);
		}
		if (CollectionUtils.isEmpty(insertStudents)) {
			throw new MarkException(ResponseCode.INIT_PROJECT_NO_STUDENT.toString());
		} else {
			saveBatch(insertStudents);
		}
	}

	@Override
	public void saveStudentConfig(String projectId, StudentExtendDTO studentDTO) {
        List<PjConfigCode> configs = studentDTO.getConfigs();
        if(CollectionUtils.isEmpty(configs)){
            return;
        }
        List<StudentConfig> pjConfigCodeList = new ArrayList<>();
        for(PjConfigCode pjConfigCode : configs){
            studentDTO.setCandidateId(pjConfigCode.getCandidateId());
            StudentConfig studentConfig = new StudentConfig();
            studentConfig.setAdmissionNum(studentDTO.getAdmissionNum());
            studentConfig.setCandidateId(pjConfigCode.getCandidateId());
            studentConfig.setConfigCode(pjConfigCode.getConfigCode());
            studentConfig.setConfigId(pjConfigCode.getCjConfigID());
            studentConfig.setConfigValue(pjConfigCode.getConfigValue());
            studentConfig.setProjectId(studentDTO.getProjectId());
            // 保存一些主要的配置信息
            if("性别".equals(pjConfigCode.getConfigCode())){
                studentDTO.setSex(pjConfigCode.getConfigValue());
            }
            if("证件号码".equals(pjConfigCode.getConfigCode())){
                studentDTO.setIdNo(pjConfigCode.getConfigValue());
            }
            if("手机号".equals(pjConfigCode.getConfigCode())){
                studentDTO.setTelephone(pjConfigCode.getConfigValue());
            }
            if("证件照".equals(pjConfigCode.getConfigCode()) || "个人照片".equals(pjConfigCode.getConfigCode())){
                String configValue = pjConfigCode.getConfigValue();
                if(!StringUtils.isEmpty(configValue)){
                    studentDTO.setAvatar(configValue.split(",")[0]);
                }
            }
            pjConfigCodeList.add(studentConfig);

        }
        studentConfigService.saveBatch(pjConfigCodeList);
		
	}

	@Override
	public Student getByAdmissionNum(String projectId, String subjectId, String admissionNum) {
		return pjDataStudentMapper.getByAdmissionNum(projectId, subjectId, admissionNum);
	}

	@Override
	public void reduceExamNum(String projectId, String admissionNum) {
		List<NeedReduceDTO> needReduceList = pjDataStudentMapper.getNeedReduce(projectId, admissionNum);
		//更新科目实考人数
		for (NeedReduceDTO needReduce : needReduceList) {
			if (pjDataSubjectMapper.updateExamNum(projectId, needReduce.getSubjectId(), needReduce.getExamNum()) != 1) {
				throw new MarkException(ResponseCode.UPDATE_STUDENTNUM_ERROR.toString());
			}
		}
	}
}
